const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasUserInfo: false,
        userInfo: {
            avatarUrl: '/images/头像.png',
            nickName: '红小豆'
        },
    },

    //   getUserProfile : function(e){
    //     wx.getUserProfile({
    //       desc: '获取您的微信个人信息',
    //       success:(res)=>{
    //           this.setData({
    //             userinfo:res.userInfo,
    //             hasUserInfo:true
    //           })
    //           wx.setStorageSync('userinfo', res.userInfo)
    //       },
    //       fail:function(e){
    //           wx.showToast({
    //             title: '你选择了取消',
    //             icon: "none",
    //             duration: 1500,
    //             mask: true
    //           })
    //       }
    //     })
    // },


    getUserProfile: function () {
        wx.getUserProfile({
            desc: '获取用户信息',
            success: (res) => {
                console.log(res);
                var openid = wx.getStorageSync('openid')
                console.log(openid)
                var user = res.userInfo
                console.log(user)
                app.globalData.userInfo = user
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true,
                    nickName: res.userInfo.nickName,
                    avatarUrl: res.userInfo.avatarUrl,
                })
                wx.cloud.database().collection('login_users').add({
                        data: {
                            avatarUrl: res.userInfo.avatarUrl,
                            nickName: res.userInfo.nickName,
                            mysubscribe: [],
                            like: [
                                [],
                                [],
                                [],
                                [],
                                [],
                                [],
                                []
                            ]
                        },
                    })
                    .then(res => {
                        wx.cloud.database().collection('login_users')
                            .get()
                            .then(res => {
                                var u = res.data.slice(-1)
                                wx.setStorageSync('openid', u[0]._openid);
                                wx.setStorageSync('like', u[0].like);
                            })
                            .catch(res => {
                                console.log("数据库读取失败");
                            })
                    })
                // this.onLoad()
                wx.setStorageSync('userInfo', res.userInfo);
                var that = this
                setTimeout(function () {
                    wx.cloud.database().collection('login_users').get()
                        .then(res => {
                            console.log(res.data[0])
                            var myname = res.data[0].nickName
                            if (myname == '微信用户') {
                                wx.navigateTo({
                                    url: '../my/fillin/fillin',
                                })
                            } else {
                                that.setData({
                                    nickName: myname,
                                    avatarUrl: res.data[0].avatarUrl
                                })
                                app.globalData.userInfo.nickName = myname
                                app.globalData.userInfo.avatarUrl = res.data[0].avatarUrl
                                wx.setStorageSync('userInfo', app.globalData.userInfo);
                                console.log(wx.getStorageSync('userInfo'))
                            }

                        })
                        .catch(err => {
                            console.log('请求失败', err)
                        })
                }, 500)
            }
        })
        if (this.data.userInfo.nickName == '微信用户') {
            wx.navigateTo({
                url: '../my/fillin/fillin',
            })
        } else {
            this.setData({
                userInfo: app.globalData.userInfo
            })
        }

    },
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
        console.log(this.data.activeNames)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(wx.getStorageSync('userInfo'))
        console.log(wx.getStorageSync('nickName'))
        console.log(wx.getStorageSync('avatarUrl'))

        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            nickName: wx.getStorageSync('nickName'),
            avatarUrl: wx.getStorageSync('avatarUrl')
        })
        console.log('xxx')
        console.log(this.data.userInfo.nickName)
        if (this.data.userInfo.nickName == '微信用户') {
            wx.navigateTo({
                url: '../my/fillin/fillin',
            })
        }
        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            })
        }
        wx.setStorageSync('userInfo', app.globalData.userInfo);
        const userInfo = wx.getStorageSync('userInfo');
        console.log(userInfo, !!userInfo);
        this.setData({
            hasUserInfo: !!userInfo,
            userInfo: app.globalData.userInfo,
            nickName: app.globalData.userInfo.nickName,
            avatarUrl: app.globalData.userInfo.avatarUrl,
        })
    },

    mypost() {
        if (this.data.userInfo) {
            {
                wx.navigateTo({
                    url: 'mypost/mypost',
                })
            }
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }

    },

    mydianzan() {
        if (this.data.userInfo) {
            {
                wx.navigateTo({
                    url: 'dianzan/dianzan',
                })
            }
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }
    },

    mylike() {
        if (this.data.userInfo) {
            {
                wx.navigateTo({
                    url: 'like/like',
                })
            }
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }
    },

    fillin() {
        {
            wx.navigateTo({
                url: 'fillin/fillin',
            })
        }
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            nickName: wx.getStorageSync('nickName'),
            avatarUrl: wx.getStorageSync('avatarUrl')
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () { // 下拉刷新
        var page = getCurrentPages().pop(); // 得到这个页面对象
        page.onLoad(); // 调用页面的onLoad()方法进行刷新页面
        wx.stopPullDownRefresh() // 刷新成功后停止下拉刷新
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})